package com.dragonfly.shutter.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Album;
import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.services.MovePictureService;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 移动图片控制类
 * @author 黄挺
 *
 */
@Controller
@RequestMapping("/movePicture.html")
public class MovePictureController {
	@Autowired
	MovePictureService movePictureService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session,
			@RequestParam(value="pictureID", required=false) String pictureID,
			@RequestParam(value="ownerID", required=false) String ownerIDStr){
		Integer albumID = (Integer) session.getAttribute("albumID");
		Integer userID = (Integer) session.getAttribute("userID");
		Message message = null;
		
		//未知的用户
		if(userID == null){
			message = new Message("您尚未登录，请先登录", "未登录", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		//未知的相册
		if(albumID == null){
			message = new Message("未知的相册", "未登录", "user.html?userID="+userID);
			return new ModelAndView("message", "message", message);
		}
		
		//空的参数
		if(pictureID == null || ownerIDStr == null ){
			message = new Message("空的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		//非法的参数
		if( !KeywordFilter.isNumeric(ownerIDStr) || !KeywordFilter.isNumeric(pictureID)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		List<Album> albums = movePictureService.getAlbumDAO().selectAlbumByUserID(userID);
		return new ModelAndView("movePicture", "albums", albums);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView OnSubmit(HttpSession session, HttpServletRequest request,
			@RequestParam(value="pictureID", required=false) String pictureID,
			@RequestParam(value="ownerID", required=false) String ownerIDStr,
			@RequestParam(value="desAlbumID", required=false) String desAlbumIDStr){
		Integer albumID = (Integer) session.getAttribute("albumID");
		Integer userID = (Integer) session.getAttribute("userID");
		Message message = null;
		
		//未知的用户
		if(userID == null){
			message = new Message("您尚未登录，请先登录", "未登录", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		//未知的相册
		if(albumID == null){
			message = new Message("未知的相册", "操作失败", "user.html?userID="+userID);
			return new ModelAndView("message", "message", message);
		}
		
		//空的参数
		if(pictureID == null || ownerIDStr == null ||
				desAlbumIDStr == null){
			message = new Message("空的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "messgae", message);
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pictureID) ||
				!KeywordFilter.isNumeric(ownerIDStr) ||
				!KeywordFilter.isNumeric(desAlbumIDStr)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		Integer ownerID = Integer.parseInt(ownerIDStr);
		Integer desAlbumID = Integer.parseInt(desAlbumIDStr);		
		
		//权限错误
		if(ownerID != userID){
			message = new Message("你不是这张图片的所有者", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		//不能将相册移动到同一相册
		if(albumID == desAlbumID){
			message = new Message("不能将相册移动到同一相册", "操作失败", "picture.html?picture="+pictureID);
			return new ModelAndView("message", "message", message);
		}
		
		movePictureService.doService(userID, pictureID, albumID, desAlbumID, request.getRequestURL().toString());
		return new ModelAndView("redirect:/picture.html?pictureID=" + pictureID);
	}
}
